package com.home.job.zh1.job4;


import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {

    private static int[] WEEK = {
      0, 7, 1, 2, 3, 4, 5, 6
    };

    public static boolean isLeapYear(int year) {
        return year % 4 == 0 && year % 100 != 0 || year % 400 == 0;
    }

    public static boolean isValidDate(int year, int month, int day) {
        if (year < 1 || year > 9999) {
            return false;
        }
        if (month < 1 || month > 12) {
            return false;
        }
        if (day < 1) {
            return false;
        }
        boolean leapYear =  isLeapYear(year);
        switch (month) {
            case 1, 3, 5, 7, 8, 10, 12 -> {
                if (day > 31) {
                    return false;
                }
            }
            case 4, 6, 9, 11 -> {
                if (day > 30) {
                    return false;
                }
            }
            case 2 -> {
                if (leapYear) {
                    if (day > 29) {
                        return false;
                    }
                } else {
                    if (day > 28) {
                        return false;
                    }
                }
            }
        }
        return  true;
    }

    public static int getDayOfWeek(int year, int month, int day) {
        try {
            if (isValidDate(year, month, day)){
                String dateString = year + "-" + month + "-" + day;
                SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd");
                Date date =sdf.parse(dateString);
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(date);
                //   calendar.get(Calendar.DAY_OF_WEEK) 周日 返回1   周六 返回7
                return WEEK[calendar.get(Calendar.DAY_OF_WEEK)];
            } else {
                return 0;
            }
        }catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    public static void printCalendar(int year, int month) {
        String[] heard = { "", "MON", "TUE", "WED", "THU", "FRI", "SAT", "SUN" };
        Calendar calendar = Calendar.getInstance();
        calendar.set(year, month, 0);
        int days = calendar.get(Calendar.DAY_OF_MONTH);
        int week = getDayOfWeek(year, month, 1);

        for (String s : heard) {
            if (!"".equals(s)){
                System.out.print(s);
                System.out.print("\t");
            }
        }
        System.out.println();
        int day = 1;
        for (int i = 0; i < 6; i++) {
            for (int j = 0; j < 7; j++) {
                // 打印第一行的时候，空出前date个空间
                if (i == 0 && j < week - 1) {
                    System.out.print("\t");
                    // 跳出当前次循环
                    continue;
                }
                if (day > days) {
                    break;
                }
                System.out.print(day + "\t");
                day++;
            }
            // 打印完一行后换行
            System.out.println();

        }
    }

    public static void printCalendar(int year) {
        for (int i = 1; i <= 12; i++) {
            System.out.println("第 " +i + " 月");
            printCalendar(year, i);
            System.out.println();
        }
    }
    public static String formatDate(int year, int month, int day) {
        if (isValidDate(year, month, day)){
            String[] week = { "", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" };
            String[] yue = { "", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
            int weekDay = getDayOfWeek(year, month, day);
            String format = "%s %s %s %s";
            return String.format(format,  week[weekDay], day < 10 ? "0" + day : day, yue[month], year);
        } else {
            System.out.println("请输入正确的日期");
            return "";
        }
    }

    public static void main(String[] args) {
        System.out.println(isLeapYear(2021));
        System.out.println(isValidDate(2021, 12, 31));
        System.out.println(getDayOfWeek(2020, 7, 1));
        printCalendar(2021, 2);
        printCalendar(2021);
        System.out.println(formatDate(2021,12,30));
    }
}
